package com.schedule.task.schedule;

import com.schedule.task.entity.BookBill;
import com.schedule.task.service.BookBillService;
import com.schedule.task.service.BookUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;

@Slf4j
@Component
public class AccountEntryTask {

    private final BookBillService bookBillService;
    private final BookUserService bookUserService;
    private final DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    public AccountEntryTask(BookBillService bookBillService, BookUserService bookUserService, DataSourceTransactionManager dataSourceTransactionManager) {
        this.bookBillService = bookBillService;
        this.bookUserService = bookUserService;
        this.dataSourceTransactionManager = dataSourceTransactionManager;
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void entryTask() {

        //查找已经入账了七天且status = 2 的bill账单
        log.info("开始今天任务");
        Integer setTime = (int)(System.currentTimeMillis() / 1000) - 7 * 24 * 60 * 60;
        List<BookBill> bookBills = this.bookBillService.searchBill(setTime);
        if (!bookBills.isEmpty()) {
            for(BookBill bookBill : bookBills) {
                //给用户加入钱跟积分
                //把记录改为status = 1已处理

                this.dealBill(bookBill);

                //多线程处理去掉
                //MulTasks mulTasks = new MulTasks(bookBill, bookBillService, bookUserService, dataSourceTransactionManager);
                //mulTasks.start();

            }
            log.info("今天的数据处理完成");
        } else {
            log.info("今天任务结束,没有要处理的数据");
        }
    }

    //@Transactional //使用注解时 方法需要public
    private void dealBill(BookBill bookBill)
    {
        //配置事务策略
        DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
        defaultTransactionDefinition.setName("money-transaction");
        defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        //设置状态点
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition);
        try {
            //给用户加入钱跟积分
            bookUserService.addMoneyScore(bookBill.getAmount(), bookBill.getUid());
            //把记录改为status = 1已处理

            Integer currentTime = (int)(System.currentTimeMillis() / 1000) - 7 * 24 * 60 * 60;
            bookBillService.change(bookBill.getId(),currentTime);
            dataSourceTransactionManager.commit(transactionStatus);
            log.info("完成记录ID:{}", bookBill.getId());
        } catch (Exception e) {
            dataSourceTransactionManager.rollback(transactionStatus);
            log.error("没有完成记录ID:{}", bookBill.getId());
        }
    }


//    // 同时操作一个字段不要用多线程
//    private class MulTasks extends Thread {
//        private BookBill bookBill;
//        private final BookBillService bookBillService;
//        private final BookUserService bookUserService;
//        private final DataSourceTransactionManager dataSourceTransactionManager;
//
//        private MulTasks(BookBill bookBill, BookBillService bookBillService, BookUserService bookUserService, DataSourceTransactionManager dataSourceTransactionManager) {
//            this.bookBill = bookBill;
//            this.bookBillService = bookBillService;
//            this.bookUserService = bookUserService;
//            this.dataSourceTransactionManager = dataSourceTransactionManager;
//        }
//
//
//        @Override
//        public void run() {
//            super.run();
//            this.dealBill(bookBill);
//        }
//
//        //@Transactional //使用注解时 方法需要public
//        private void dealBill(BookBill bookBill)
//        {
//            //配置事务策略
//            DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
//            defaultTransactionDefinition.setName("money-transaction");
//            defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
//            //设置状态点
//            TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition);
//            try {
//                //给用户加入钱跟积分
//                bookUserService.addMoneyScore(bookBill.getAmount(), bookBill.getUid());
//                //把记录改为status = 1已处理
//                bookBillService.change(bookBill.getId());
//                dataSourceTransactionManager.commit(transactionStatus);
//                log.info("完成记录ID:{}", bookBill.getId());
//            } catch (Exception e) {
//                dataSourceTransactionManager.rollback(transactionStatus);
//                log.info("没有完成记录ID:{}", bookBill.getId());
//            }
//        }
//    }
}
